package hadoop.mr05;


import hadoop.unit.GlobalConfiguration;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;


public class Phone_driver extends Configured implements Tool {
    private static Configuration conf = GlobalConfiguration.getCon();

    @Override
    public int run(String[] args) throws Exception {
        // 判断是否有输入路径和输出路径
        if (args.length < 2) {
            System.err.println("NO TASK");
            System.exit(2); // 输出2为异常退出
        }
        Job job = Job.getInstance(conf, "foodKinds");

        job.setJarByClass(Phone_driver.class);
        job.setMapperClass(PhoneMapper.class);

        // 输出第一个值
//        job.setReducerClass(PhoneReducer.class);

        // 输出前3个值
        job.setReducerClass(PhoneReducer_Top3.class);

        job.setMapOutputKeyClass(Phone.class);
        job.setMapOutputValueClass(Text.class);

        // todo 如果重写分组则需要进行相关配置
        job.setGroupingComparatorClass(group.class);

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(NullWritable.class);

        Path in_path = new Path(args[0]);
        FileInputFormat.setInputPaths(job, in_path);
        Path out_path = new Path(args[1]);
        FileOutputFormat.setOutputPath(job, out_path);

        FileSystem fs = FileSystem.get(conf);
        if(fs.exists(out_path)){
            fs.delete(out_path,true);
        }
        return job.waitForCompletion(true)?0:2;
    }
    public static void main(String[] args) throws Exception {
        int run = ToolRunner.run(conf, new Phone_driver(), args);
        System.exit(run);
    }
}

